//
//  CarListViewController.m
//  Runone
//
//  Created by runone01 on 16/8/24.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "CarListViewController.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "CarListModel.h"
#import "AllLastestVehicleDynamic.h"
#import "HistoryViewController.h"
#import "CarCell.h"
#import "UIView+Message.h"
#import "PromptUtil.h"
#import "SVProgressHUD.h"

@interface CarListViewController ()

@property(nonatomic,strong)CarListModel *carListModel;
@property(nonatomic,strong)UIView *MessageView;
@property (strong, nonatomic) IBOutlet UITableView *carListTableView;
//@property(nonatomic,strong)NSTimer *timer;
@end

@implementation CarListViewController


//-------------------------------------------


- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title=@"查询结果";
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text = @"查询结果";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=label;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    [self.tableView registerNib:[UINib nibWithNibName:@"CarCell" bundle:nil] forCellReuseIdentifier:@"CarCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.CarDetailArray = [NSMutableArray array];
    self.CarListArray   = [NSMutableArray array];
    [self requestCarList];
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}



-(void)requestCarList {
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    NSMutableArray *CarListArray=[[NSMutableArray alloc] init];
    __weak typeof(self) weakSelf = self;
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCarHistoryURL;
    getCurrentEventModel.DataValue=[self jsonstr];
    getCurrentEventModel.UpdatedTime=[requestHelper getCurrentTime];
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"SearchHistoryTrajectory";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        NSArray *Temp=[dataValueModel mj_JSONObject];
        if (Temp.count>0)
        {
            if (Temp.count==1) {
                self.carListModel=Temp[0];
                NSLog(@"%@",_carListModel.VehicleDynamicData);
                [CarListArray addObject:self.carListModel];
                
                self.CarListArray=CarListArray;
                for (NSDictionary *modelDic in self.carListModel.VehicleDynamicData) {
                    self.carListModel.carModel=[AllLastestVehicleDynamic mj_objectWithKeyValues:modelDic];
                    
                    [arr addObject:self.carListModel.carModel];
                    self.CarDetailArray=arr;
                }
            }
            else
            {
                for (NSDictionary *dic in Temp)
                {
                    CarListModel *model=[CarListModel mj_objectWithKeyValues:dic];
                    [CarListArray addObject:model];
                    
                    for (NSDictionary *modelDic in model.VehicleDynamicData) {
                        model.carModel=[AllLastestVehicleDynamic mj_objectWithKeyValues:modelDic];
                        
                        [arr addObject:model.carModel];
                        self.CarDetailArray=arr;
                    }
                    self.CarListArray=CarListArray;
                }

            }
            [weakSelf.carListTableView reloadData];

        }
        else
        {
            [SVProgressHUD showSuccessWithStatus:@"无此车历史轨迹"];
            [self.navigationController popViewControllerAnimated:YES];
            NSLog(@"无历史轨迹");
            return ;
        }
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return self.CarListArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        CarCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CarCell" forIndexPath:indexPath];
        AllLastestVehicleDynamic *model=self.CarDetailArray[indexPath.row];
        cell.CarNumber.text=[NSString stringWithFormat:@"%@",model.VehicleNo];
        CarListModel *Timemodel=self.CarListArray[indexPath.row];
        cell.BenginTime.text=Timemodel.BeginTime;
        cell.EndTime.text=Timemodel.EndTime;
    if ([Timemodel.Direction isEqualToString:@"1"]) {
        cell.CarType.text=@"往广州方向";
    }
    if ([Timemodel.Direction isEqualToString:@"2"]) {
        cell.CarType.text=@"往乐昌方向";
    }
    if ([Timemodel.Direction isEqualToString:@"3"]) {
        cell.CarType.text=@"往湖南方向";
    }
    if ([model.TransType isEqualToString:@"30"] || [model.TransType isEqualToString:@"31"] || [model.TransType isEqualToString:@"32"]) {
        cell.carTypeL.text = @"危化品车";
    }else {
    cell.carTypeL.text = @"营运客车";
    }
        return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 116;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSMutableArray *mutable=[[NSMutableArray alloc] init];

        CarListModel *model=_CarListArray[indexPath.row];
    
        for (NSDictionary *dic in model.VehicleDynamicData)
        {
            model.carModel=[AllLastestVehicleDynamic mj_objectWithKeyValues:dic];
            [mutable addObject:model.carModel];
        }
    
    HistoryViewController *historyVC = [[HistoryViewController alloc] init];
    historyVC.historyModel=self.CarDetailArray[indexPath.row];
    historyVC.arr=mutable;
    [self.navigationController pushViewController:historyVC animated:YES];
}
//str	__NSCFString *	@"[{\"key\":\"VehicleNo\",\"value\":\"鄂H18935\"},{\"key\":\"BegineTime\",\"value\":\"2016-12-13 16:51:56\"},{\"key\":\"EndTime\",\"value\":\"2016-12-13 16:52:01\"}]"	0x0000000158a93d40
-(NSString *)jsonstr {
    NSArray *arr=@[@{@"key":@"VehicleNo",@"value":self.carNum},@{@"key":@"BegineTime",@"value":self.beginTime},@{@"key":@"EndTime",@"value":self.endTime}];
    NSString *str=[arr mj_JSONString];
    return str;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
